//
//  WaterFallViewController.m
//  XFWaterfallFlowLayoutDemo
//
//  Created by yusaiyan on 17/4/22.
//  Copyright © 2017年 QunYu_TD. All rights reserved.
//

#import "WaterFallViewController.h"
#import "WaterFallCell.h"
#import "XFWaterfallFlowLayout.h"

@interface WaterFallViewController () <UICollectionViewDataSource, UICollectionViewDelegate, XFWaterfallFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *m_collectionView;
@property (nonatomic, strong) NSMutableArray *m_dataArray;

@end

@implementation WaterFallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil];
    NSArray *imageDics = [NSArray arrayWithContentsOfFile:path];

    [self.m_dataArray removeAllObjects];
    for (NSDictionary *dic in imageDics) {
        WaterFallModel *model = [[WaterFallModel alloc]initWithDictionary:dic];
        [_m_dataArray addObject:model];
    }
    [self.m_collectionView reloadData];
}

- (NSMutableArray *)m_dataArray {
    if (!_m_dataArray) {
        _m_dataArray = [[NSMutableArray alloc]init];
    }
    return _m_dataArray;
}

#pragma mark - UICollectionView

- (UICollectionView *)m_collectionView {
    if (!_m_collectionView) {
        
        XFWaterfallFlowLayout *flowLayout = [[XFWaterfallFlowLayout alloc] init];
        flowLayout.colMargin = 10;//间距
        flowLayout.rowMargin = 10;//间距
        flowLayout.colCount = 3;//控制列数
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);//视图边界
        flowLayout.delegate = self;
        
        _m_collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _m_collectionView.dataSource = self;
        _m_collectionView.delegate   = self;
        _m_collectionView.showsVerticalScrollIndicator = NO;
        _m_collectionView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_m_collectionView];
        
        [self.m_collectionView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
        
        [_m_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
        
        [_m_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reusableView"];
    }
    return _m_collectionView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _m_dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WaterFallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    WaterFallModel *model = [_m_dataArray objectAtIndex:[indexPath item]];
    [cell setCellData:model];
    
    return cell;
}

#pragma mark XFWaterfallFlowLayoutDelegate

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XFWaterfallFlowLayout *)collectionViewLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath {
    WaterFallModel *model = [_m_dataArray objectAtIndex:[indexPath item]];
    return model.wat_picHeigth / model.wat_picWidht * width;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(XFWaterfallFlowLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.view.bounds.size.width, 50);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(XFWaterfallFlowLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(self.view.bounds.size.width, 50);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor cyanColor];
        
        reusableView = headerView;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor brownColor];
        
        reusableView = footerView;
    }
    return reusableView;
}

@end
